package com.estar.integaration.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estar.integaration.Helper.MaterialCalcHelper;
import com.estar.integaration.Mapper.TechPushHistoryMapper;
import com.estar.integaration.Service.ICartGoodsService;
import com.estar.integaration.Service.ICartService;
import com.estar.integaration.Service.IMaterialService;
import com.estar.integaration.Service.ITechPushHistoryService;
import com.estar.integaration.Util.MExcelUtil;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.CartGoods;
import com.estar.integaration.entity.NoSQL.Res;
import com.estar.integaration.entity.ProductMachineInfo;
import com.estar.integaration.entity.TechPushHistory;
import com.google.gson.Gson;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/tech-push-history")
public class TechPushHistoryController {

    @Autowired
    TechPushHistoryMapper techPushHistoryMapper;

    @Autowired
    ITechPushHistoryService iTechPushHistoryService;

    @Autowired
    ICartGoodsService cartGoodsService;

    @Autowired
    IMaterialService materialService;

    @GetMapping("/gethistory")
    public Res gettechHistory(){
        List<TechPushHistory> historyList = techPushHistoryMapper.selectList(null);
        List<TechPushHistory> sortedHistoryList = historyList.stream()
                .sorted(Comparator
                        .comparing(TechPushHistory::getTime)
                        .reversed())
                .collect(Collectors
                        .toList());
        return new Res("20000",new Gson().toJson(sortedHistoryList));
    }



    /**
     * 功能：删除技术部提交历史
     * 流程：① 根据ID删除对应的提交历史
     *      ② 筛选出删除记录后的新提交历史
     *      ③ 排序
     * @Author HLF
     */
    @ApiOperation(value = "deleteTechHistory",tags = "删除技术部提交历史",response = TechPushHistory.class)
    @GetMapping("/deleteTph")
    public Res deleteTechHistory(@RequestParam String id){
      TechPushHistory techPushHistory = iTechPushHistoryService.getById(id);
      if(techPushHistory == null){
          return new Res("40000","未查询到本清单绑定的产品。");
      }
      iTechPushHistoryService.deleteTechPushHistoryById(id);
      //重写为重新解析此文件然后依次删除
      String address = techPushHistory.getAddress();
      ProductMachineInfo productMachineInfo = null;
      try {
        productMachineInfo = MExcelUtil.HparseExcel(address);
      }catch (IOException e){
        e.printStackTrace();
      }
      //执行删除
      MaterialCalcHelper.deletewithCalc(productMachineInfo);
      /**
      String productId = techPushHistory.getProductId();
      //购物车有的不给删
      List<CartGoods> cartGoodsList = cartGoodsService.selectCartGoodsByproductId(productId);
      if(cartGoodsList.size() > 0){
        return new Res("40010","本清单已有部分物料加入购物车,不可删除");
      }
      materialService.deleteAllByProductId(techPushHistory.getProductId());
      **/
      return gettechHistory();
//        return new Res("20000",new Gson().toJson(sortedTphList));

    }

    /**
     * 功能： 下载技术部提交的文件
     * @param id
     * @return
     */
  @GetMapping("/getExcel")
  public ResponseEntity<FileSystemResource> getExcel(@RequestParam String id){
    TechPushHistory techPushHistory = iTechPushHistoryService.getOne(new QueryWrapper<TechPushHistory>().eq("id",id));
    File file = new File(techPushHistory.getAddress());
    if(file.exists()){
      return Util.exportExcel(file);
    }
    return null;
  }
}
